package net.expectx.gw.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.common.base.BaseResultConstants;
import net.expectx.common.util.StringUtil;
import net.expectx.gw.bean.ContentArticle;
import net.expectx.gw.bean.ContentCategory;
import net.expectx.gw.bean.CustomerRequirement;
import net.expectx.gw.service.ContentArticleService;
import net.expectx.gw.service.ContentCategoryService;
import net.expectx.gw.service.CustomerRequirementService;
import net.expectx.gw.util.AdminConstants;
import net.expectx.gw.util.BaseResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * controller
 * @author lijian
 * @date 2019/1/18
 */
@Controller
@RequestMapping("/manager/officialWebsite")
@Api(value = "控制器", description = "管理")
public class OfficialWebsiteController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OfficialWebsiteController.class);
    @Autowired
    ContentArticleService contentArticleService;
    @Autowired
    ContentCategoryService contentCategoryService;
    @Autowired
    CustomerRequirementService customerRequirementService;

    @ApiOperation(value = "客户")
    @RequestMapping(value = "/client", method = RequestMethod.GET)
    @ResponseBody
    public Object client(String type,String category) {
        Map<String, Object> map = new HashMap<>(2);
        String StrWhere=" category_id in ( select id from content_category where type =2)";
        List<ContentArticle> clients = contentArticleService.findByAttr(StrWhere, null);
        List<ContentCategory> contentCategories = contentCategoryService.findByAttr("type =2", null);
        List<ContentCategory> contentCategories1 = contentCategoryService.findByAttr("type =3", null);
        map.put("clients", clients);
        map.put("industry", contentCategories);
        map.put("type", contentCategories1);

        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, map), HttpStatus.OK);

    }
    @ApiOperation(value = "客户详情")
    @RequestMapping(value = "/clientDetail", method = RequestMethod.GET)
    @ResponseBody
    public Object clientDetail(String id) {
        ContentArticle client = contentArticleService.selectByPrimaryKey(id);
        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, client), HttpStatus.OK);

    }
    @ApiOperation(value = "解决方案")
    @RequestMapping(value = "/solution", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> solution() {
        Map<String, Object> map = new HashMap<>(2);
        List<ContentCategory> clients = contentCategoryService.findByAttr("  type =3", "id");

        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, clients), HttpStatus.OK);

    }
    @ApiOperation(value = "解决方案详情")
    @RequestMapping(value = "/solutionDetail", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> solutionDetail(String id) {
        Map<String, Object> map = new HashMap<>(2);
        ContentArticle solutionDetail = new ContentArticle();
        if (StringUtils.isNotBlank(id)) {
            solutionDetail = contentArticleService.selectByPrimaryKey(id);
        }else{
            solutionDetail = contentArticleService.selectByPrimaryKey(120);
        }

        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, solutionDetail), HttpStatus.OK);

    }
    @ApiOperation(value = "软件技术")
    @RequestMapping(value = "/technologyService", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> technologyService() {
        Map<String, Object> map = new HashMap<>(2);
        List<ContentCategory> technologyService = contentCategoryService.findByAttr("  type =4", "id");

        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, technologyService), HttpStatus.OK);

    }
    @ApiOperation(value = "解决方案详情")
    @RequestMapping(value = "/technologyServiceDetail", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> technologyServiceDetail(String id) {
        Map<String, Object> map = new HashMap<>(2);
        ContentArticle technologyServiceDetail = new ContentArticle();
        if (StringUtils.isNotBlank(id)) {
            technologyServiceDetail = contentArticleService.selectByPrimaryKey(id);
        }else{
            technologyServiceDetail = contentArticleService.selectByPrimaryKey(111);
        }

        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, technologyServiceDetail), HttpStatus.OK);

    }
    @ApiOperation(value = "客户分类")
    @RequestMapping(value = "/customerType", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> customerType(String id) {
        Map<String, Object> map = new HashMap<>(2);
        ContentArticle technologyServiceDetail = new ContentArticle();
        if (StringUtils.isNotBlank(id)) {
            technologyServiceDetail = contentArticleService.selectByPrimaryKey(id);
        }else{
            technologyServiceDetail = contentArticleService.selectByPrimaryKey(111);
        }

        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, technologyServiceDetail), HttpStatus.OK);

    }
    @ApiOperation(value = "新闻")
    @RequestMapping(value = "/news", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> news() {
        Map<String, Object> map = new HashMap<>(2);
        List<ContentArticle> news = contentArticleService.findByAttr(" category_id in( select id from content_category where type =1)  ", "gmt_create");

        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, news), HttpStatus.OK);

    }
    @ApiOperation(value = "新闻详情")
    @RequestMapping(value = "/newsDetail", method = RequestMethod.GET)
    @ResponseBody
    public Object newsDetail(String id) {
        ContentArticle client = contentArticleService.selectByPrimaryKey(id);
        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, client), HttpStatus.OK);

    }

    @ApiOperation(value = "添加客户需求")
    @RequestMapping(value = "/createReauirement", method = RequestMethod.POST)
    @ResponseBody
    public Object createReauirement(CustomerRequirement customerRequirement) {
        customerRequirement.setGmtCreate(new Date());
        customerRequirement.setGmtModified(new Date());
        customerRequirement.setIsDeleted(AdminConstants.NO);
        return customerRequirementService.insertSelective(customerRequirement);
    }
    @ApiOperation(value = "页尾栏位")
    @RequestMapping(value = "/footData", method = RequestMethod.GET)
    @ResponseBody
    public Object footData() {
        Map<String, Object> map = new HashMap<> ();
        List<ContentArticle> solution = contentArticleService.findByAttr("category_id in (select id from content_category where type =3)","gmt_create");
        List<ContentArticle>  technology = contentArticleService.findByAttr("category_id =49", null);
        List<ContentArticle>  services = contentArticleService.findByAttr("category_id =50", null);
        map.put("solution",solution );
        map.put("technology",technology );
        map.put("services",services );
        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, map), HttpStatus.OK);
    }
}
